﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLogic;
using System.Configuration;

namespace BusinessLogic
{
    [Serializable]
    public class Job : CodeTable<Job>
    {
        #region Fields
        private DateTime _userDate;
        private List<ServiceReport> _serviceReports;
        private Constituent _location;
        private Constituent _billTo;
        private bool _sentToIan;
        private bool _sentToJenny;
        private bool _archived;
        private DateTime _archivedDate;
        #endregion

        #region Methods
        public override void Create()
        {
            this._id = JobDataAccess.Create(this);
        }

        public override void Delete()
        {
            JobDataAccess.Delete(this.ID);
        }

        public static void Finalize(Guid jobID)
        {
            JobDataAccess.Finalize(jobID);
        }

        public static List<Job> GetAllUnarchived()
        {
            return JobDataAccess.GetAllUnarchived();
        }

        protected override List<Job> ProtectedGetAll()
        {
            return JobDataAccess.GetAll();
        }

        protected override Job ProtectedGetByID(Guid id)
        {
            Job j = JobDataAccess.GetByID(id);
            j.ServiceReports = ServiceReport.GetAllSummariesByJobID(id);

            j.ServiceReports.ForEach(sr => sr.Archived = j.Archived);
            j.ServiceReports.ForEach(sr => sr.JobID = j.ID);

            return j;
        }

        public static void SendToIan(Guid jobID)
        {
            JobDataAccess.SendToIan(jobID);

            string to = ConfigurationManager.AppSettings["Ian"].ToString();
            SendToEmail(to, jobID);
        }

        public static void SendToJenny(Guid jobID)
        {
            JobDataAccess.SendToJenny(jobID);

            string to = ConfigurationManager.AppSettings["Jenny"].ToString();
            SendToEmail(to, jobID);
        }

        private static string SendToEmail(string recipient, Guid jobID)
        {
            string result = string.Empty;

            try
            {
                string body = string.Format(SEPDefaults.Other.EmailJobSentToBody, jobID.ToString());                
                Common.Email.Send(recipient, "no-reply@southeastpathology.com", SEPDefaults.Other.EmailSentToSubject, body, true); 
            }
            catch(Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

        public override void Update()
        {
            JobDataAccess.Update(this);
        }
        #endregion

        #region Properties
        public override string CacheKeyName
        {
            get { return "JOBS"; }
        }

        public DateTime UserDate
        {
            get
            {
                return _userDate;
            }
            set
            {
                _userDate = value;      
            }
        }

        public string UserDateAsString
        {
            get
            {
                if(_userDate != null)
                {
                    return _userDate.Date.ToShortDateString();
                }
                
                return string.Empty;
            }
        }

        public List<ServiceReport> ServiceReports
        {
            get
            {
                return _serviceReports;
            }
            set
            {
                _serviceReports = value;
            }
        }

        public int NumberOfServiceReports
        {
            get
            {
                if(_serviceReports != null)
                {
                    if(_serviceReports.Count < _serviceReports.Capacity)
                    {
                        return _serviceReports.Capacity;
                    }
                    else
                    {
                        return _serviceReports.Count;
                    }
                }
                else
                {
                    return 0;
                }
            }
        }

        public Constituent Location
        {
            get
            {
                return _location;
            }
            set
            {
                _location = value;
            }
        }

        public Constituent BillTo
        {
            get
            {
                return _billTo;
            }
            set
            {
                _billTo = value;
            }
        }

        public bool SentToIan
        {
            get
            {
                return _sentToIan;
            }
            set
            {
                _sentToIan = value; 
            }
        }

        public bool SentToJenny
        {
            get
            {
                return _sentToJenny;
            }
            set
            {
                _sentToJenny = value;
            }
        }

        public bool Archived
        {
            get
            {
                return _archived;
            }
            set
            {
                _archived = value;
            }
        }

        public DateTime ArchiveDate
        {
            get
            {
                return _archivedDate;
            }
            set
            {
                _archivedDate = value;
            }
        }

        public string ArchiveDateAsString
        {
            get
            {
                if(_archivedDate != null)
                {
                    if(!_archivedDate.Equals(DateTime.MinValue))
                    {
                        return _archivedDate.Date.ToShortDateString();
                    }
                }

                return string.Empty;
            }
        }

        public string FullName
        {
            get 
            {
                return string.Concat(this.Description, " ", this.UserDateAsString).Trim();
            }
        }
        #endregion
    }
}